﻿using System;
using System.IO;

namespace com.kmxy.MonitorModel.Common
{
    public class WriteLogsFile
    {
        /// <summary>
        /// 文件路径
        /// </summary>
        public string FilePath { get; set; }

        /// <summary>
        /// 文件名称
        /// </summary>
        public string FileName { get; set; }

        public WriteLogsFile(string filePath, string fileName)
        {
            this.FilePath = filePath;
            this.FileName = fileName;
        }

        /// <summary>
        /// 写系统日志
        /// </summary>
        /// <param name="logs"></param>
        public void writeSystemLogs(string logs)
        {
            try
            {
                //string path = string.Format(@"{0}\YZF_LOG\MainServer\", AppDomain.CurrentDomain.BaseDirectory);
                var path = FilePath;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                var filePath = $@"{path}\{FileName}-{DateTime.Now.ToString("yyyyMMdd")}.txt";
                using (var sw = new StreamWriter(filePath, true))
                {
                    sw.Write($"Time:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}\r\n");
                    sw.Write($"\t{logs}{Environment.NewLine}");
                    sw.Flush();
                }
            }
            catch (Exception)
            {
                //Console.WriteLine(e.ToString() + logs);
            }
        }

        public void writeSystemLogs(string format, params object[] args)
        {
            writeSystemLogs(string.Format(format, args));
        }
    }
}
